// pages/newslists/newslists.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    pageNum: 1,
    pageSize: 10,
    news: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    this.setData({
      news: [],
      pageNum: 1
    })
    that.refresh(that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.lower()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  lower() {
    let isData = this.data.isData;
    let that = this;
    if (isData) {
      return;
    }
    this.refresh(that, that.data.pageNum, that.data.pageSize);
  },
  refresh(that, pageNum, pageSize) {
    pageNum = pageNum || 1;
    pageSize = pageSize || 10;
    app.globalData.wxRequest.requestGet('data/getInformation.do', {
      pageNum: pageNum,
      pageSize: pageSize
    }).then(res => {
      console.log(res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if (res.code == 200) {
        let news = that.data.news
        let list = news.concat(res.data.result);
        let isData = false;
        if (res.data.result.length < res.data.pageSize || res.data.pageSize * res.data.pageNum >= res.data.total) {
          isData = true;
        }
        that.setData({
          news: list,
          pageNum: res.data.pageNum + 1,
          pageSize: res.data.pageSize,
          isData: isData
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })
  },
  navToNews(e) {
    wx.setStorage({
      key: 'jumpUrl',
      data: e.currentTarget.dataset.url,
      success(res) {
        wx.navigateTo({
          url: '/pages/newslists/newsDetail/newsDetail',
        })
      }
    })
  },
})